package tmi.li.txwthr;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class TxWthrReceiver extends BroadcastReceiver{
	static String TAG = "tmi.li.txwthr.TxWthrReceiver";
	
	@Override
    public void onReceive(Context context, Intent intent) {
		
		//getting the Broadcast Action to verify the intent Actions
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_BOOT_COMPLETED) 
        	|| action.equals(Intent.ACTION_SCREEN_ON) 
        	|| action.equals("tmi.li.txwthr.UPDATE")    ) {
        	
        	//get the app widgetmanager instance
            AppWidgetManager gm = AppWidgetManager.getInstance(context);
            
            //getting the ids
            TxPref txpref = new TxPref(TxPref.PREFS_NAME_TXWTHR);
            String ids = txpref.getIds(context);
            
            //if ids isnt null and contains a semicolon split the string and update each widget
            if(ids != null && ids.contains(";")) {
            	
            	String[] appWidgetIds = ids.split(";");
            	for(int i = 0; i < appWidgetIds.length; i++) {
            		TxWthrProvider.updateWidget(context, gm, Integer.valueOf(appWidgetIds[i]));
            	}
            	
            }else if(ids != null){
            	//update the widget
            	TxWthrProvider.updateWidget(context, gm, Integer.valueOf(ids));
            	
            }else {
            	//FIXME
            }
        }
    }
}
